package com.hww.dtalk;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.google.common.collect.Maps;

import net.gdface.utils.FaceUtilits;
import net.gdface.utils.NetworkUtil;

import java.util.HashMap;

import gu.dtalk.engine.DeviceUtils;
import gu.dtalk.engine.ItemEngine;
import gu.dtalk.engine.SampleConnector;
import gu.dtalk.redis.DefaultCustomRedisConfigProvider;
import gu.dtalk.redis.RedisConfigType;
import gu.simplemq.Channel;
import gu.simplemq.redis.JedisPoolLazy;
import gu.simplemq.redis.JedisUtils;
import gu.simplemq.redis.RedisFactory;
import gu.simplemq.redis.RedisSubscriber;
import redis.clients.jedis.Protocol;

import static gu.dtalk.CommonUtils.getConnChannel;

public class MainActivity extends AppCompatActivity {
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.main_tv_show_mac);

        ThreadPoolManager.getInstance().getMoreExecutorService().execute(new Runnable() {
            @Override
            public void run() {

                try {
                    RedisConfigType configType = RedisConfigType.CLOUD;
                    Log.e("hww__", "configType= " + configType.toString());
                    // 创建redis连接实例
                    JedisPoolLazy.createDefaultInstance(configType.readRedisParam());

                    JedisPoolLazy pool = JedisPoolLazy.getInstance(configType.readRedisParam(), false);
                    RedisSubscriber subscriber = RedisFactory.getSubscriber(pool);

                    ControlMenu root = new ControlMenu().init().register(DemoListener.INSTANCE);
                    byte[] devMac = DeviceUtils.DEVINFO_PROVIDER.getMac();
                    SampleConnector connAdapter = new SampleConnector(pool).setSelfMac(FaceUtilits.toHex(devMac)).setItemAdapter(new ItemEngine(pool).setRoot(root));

                    System.out.printf("hww__ DEVICE MAC address(设备地址): %s\n", NetworkUtil.formatMac(devMac, ":"));
                    String connChannel = getConnChannel(devMac);
                    Channel<String> connch = new Channel<>(connChannel, String.class).setAdapter(connAdapter);
                    subscriber.register(connch);
                    System.out.printf("hww__ Connect channel registered(连接频道注册) : %s \n", connChannel);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv.setText(NetworkUtil.formatMac(devMac, ":"));
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    mLog("e== " + e.toString());
                }
            }
        });

    }

    private HashMap<JedisPoolLazy.PropName, Object> getRedisParam(String ip) {
        HashMap<JedisPoolLazy.PropName, Object> redisParameters = Maps.newHashMap();
        redisParameters.put(JedisPoolLazy.PropName.host, ip);
        redisParameters.put(JedisPoolLazy.PropName.port, 26416);
        redisParameters.put(JedisPoolLazy.PropName.password, "86a1b907d54bf7010394bf316e183e67");
        redisParameters.put(JedisPoolLazy.PropName.database, 0);
        /*redisParameters.put(JedisPoolLazy.PropName.timeout, 2000);*/
        HashMap<JedisPoolLazy.PropName, Object> paramet = JedisUtils.initParameters(redisParameters);
        mLog(paramet.toString());
        return paramet;
    }

    public void mLog(String log) {
        Log.i("hww__", "" + log);
    }
}
